package com.example.bank.exception;

import com.auth0.jwt.exceptions.JWTDecodeException;
import com.auth0.jwt.exceptions.TokenExpiredException;
import com.example.bank.domain.Result;
import com.example.bank.enums.ResultEnum;
import com.example.bank.util.ResultUtil;
import org.apache.shiro.ShiroException;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import javax.servlet.http.HttpServletRequest;

/**
 * @author ：Leeziqiang
 * @description：TODO
 * @date ：2021/11/13 10:02
 */

@RestControllerAdvice
public class SystemExceptionHandler {

    // 捕捉shiro异常
    @ExceptionHandler(ShiroException.class)
    public Object handleShiroException(ShiroException e) {
        return ResultUtil.error(401, "您没有权限!");
    }

    // 捕捉其他异常
    //@ExceptionHandler(Exception.class)
    public Result globalException(HttpServletRequest request, Throwable exception) {
        return ResultUtil.error(ResultEnum.ERROR.getCode(), exception.getMessage());
    }

    @ExceptionHandler(JWTDecodeException.class)
    public Result handleJWTDecodeException(JWTDecodeException e) {
        return ResultUtil.error(500, "Token异常");
    }

    @ExceptionHandler(TokenExpiredException.class)
    public Result handleTokenExpireException(TokenExpiredException e) {
        return ResultUtil.error(500, "Token过期");
    }

    @ExceptionHandler(SystemException.class)
    public Result handlerException(SystemException e) {
        return ResultUtil.error(e.getMessage());
    }

}
